define('sysmenu/page',function(){
	var ViewModel = function (){
		toastr.options = {
				  "closeButton": true,
				  "debug": false,
				  "progressBar": true,
				  "positionClass": "toast-top-full-width",
				  "onclick": null,
				  "showDuration": "300",
				  "hideDuration": "1000",
				  "timeOut": "3000",
				  "extendedTimeOut": "1000",
				  "showEasing": "swing",
				  "hideEasing": "linear",
				  "showMethod": "fadeIn",
				  "hideMethod": "fadeOut"
				}
		var self=this;
		var url ="/sysmenu/doFindObjects";
		var params={
				"pageSize":'',
				"pageIndex":''
		};
		//第一个参数请求路径，第二个是传入的参数，第三个是回调函数
		self.DataGrid = new RIT.eW.DataGridAjax(url, params, self.finMenu);
		self.finMenu = function(result){
			var data = result;
			if(data && data.length && data.length>0){
				$.each(data, function (index, val) { val.checked = false; });
			}
			var items = ko.mapping.fromJS(data);
			return items();
		}	
		//隐藏展开搜索框
		self.showText = ko.observable('收缩');
		self.showorno = ko.observable(true);
		self.noshow= function(){
			if(self.showText() =='收缩' ){
				self.showText('展开');
				self.showorno(false);
			}else{
				self.showText('收缩');
				self.showorno(true);
			}
		}
	/*	self.prentZtree = function(){
			$("#txt_dep1").focus(function(){
				$("#treeContent2").show();
				self.ztreeshow();
				self.ztreeblur();
			});
		}*/
		/**定义弹窗的名字*/
		var title1 = "";
		//页面重置按钮
		self.reset = function(){
			self.page({
				pageName:'',
				prentName:'',
				isEnable:''
			});
		};
		/**修改的弹窗*/
		self.addSysmenu=ko.observable();
		self.update = function(id){
			console.log(id.isenable)
			title1 = "修改";
			self.addSysmenu(id);
			$("#txt_dep").focus(function(){
				$("#treeContent").show();
				self.ztreeshow();
				self.ztreeblur();
			});
			$("#iconUrl").focus(function(){
				$("#iconKunhide").show();
				self.iconblur();
			});
			self.layer();
		}
		//新增弹出添加框
		self.show_add=function(){
			title1 = "添加";
			self.addSysmenu({
				pname:'',
				name:'',
				pid:'',
				icon:'',
				sortcode:'',
				valid:'',
				pageaddress:'',
				comment:'',
				isenable:''
			});
			//父节点获取焦点事件
			$("#txt_dep").focus(function(){
				$("#treeContent").show();
				self.ztreeshow();
				self.ztreeblur();
			});
			//图片获取焦点事件
			$("#iconUrl").focus(function(){
				$("#iconKunhide").show();
				self.iconblur();
			});
			self.layer();
		};
		/**弹窗封装*/
		self.layer = function(){
			window.layer.open({
				type : 1,
				title:title1,
				skin : 'layui-layer-rim', //加上边框
				area : [ '698px', '517px' ], //宽高
				content :$('#popOrg')
			});
		};


		//动态绑定值
		self.page =ko.observable({
			pageName:'',
			prentName:'',
			isEnable:''
		});
		/**查询（查询按钮）*/
		self.search=function(){
			var data=self.page();
			console.log(data);
			self.DataGrid.GridParams.pageIndex(1);
			self.DataGrid.GetData(data);
		};
		/**根据id删除*/
		self.deleteId = function(data){
			layer.confirm('确定删除吗？', {
				btn: ['确定','取消'] //按钮
			}, function(){   //点击确定访问后台
				var url = "/sysmenu/doDeleteObject";
				$.ajax({
					url:url,
					data:JSON.stringify(data.id),
					type:"post",
					dataType:"json",
					contentType:"application/json;charset=UTF-8",
					success:function(result){
						if(result.data == 1){
							//layer.msg('已删除', {icon: 1});
							toastr.error("删除成功");
							self.layerClose();
							self.search();
						}else{
							toastr.error(result.message);
							self.layerClose();
							self.search();
						}
					}
				});
			},
			function(){   
				toastr.success("已取消");
			});
		}
		/*禁用或者恢复*/
		self.pr = function(data){
			layer.confirm('确定修改状态吗？', {
				btn: ['确定','取消'] //按钮
			}, function(){   //点击确定访问后台
				var url = "/sysmenu/doDeleteObject";
				$.ajax({
					url:"/sysmenu/doUpdataIsenable",
					data:JSON.stringify(data),
					dataType:"json",
					type:"post",
					contentType:"application/json;charset=UTF-8",
					success:function(result){
						if(result.data == 0){
							toastr.error("修改状态失败!");
						}
						if(result.data ==1){
							toastr.success("修改状态成功!");
							self.layerClose();
							self.search();
						}
					}
				});
			},
			function(){   
				toastr.success("已取消");
			});
			
		}
		/**修改和添加*/
		self.submitMenu = function(data){
			var data = self.addSysmenu();
			if(node != null){
				self.addSysmenu().pid = node[0].id;
			}else{
				self.addSysmenu().pid = '';
			}
			self.addSysmenu().pname = $("#txt_dep").val();
			console.log(self.addSysmenu().pname);
			self.addSysmenu().icon=$("#iconUrl").val();
			console.log(data.pname);
			var saveObjectUrl = "/sysmenu/doSaveObject";
			var updateObjectUrl = "/sysmenu/doUpdateObject";
			//根据有没有id值判断请求路径
			var url = data.id?updateObjectUrl:saveObjectUrl;
			//发起请求
			$.ajax({
				url:url,
				data:JSON.stringify(data),
				type:"post",
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				success:function(result){
					if(result.data == 1){
						toastr.success("保存成功!");
					}else{
						layer.msg(result.message,{
							time:2000,
						});
					}
					self.search();
				}
			});
			self.layerClose();
		}
		/**关闭layer弹窗*/
		self.layerClose = function(){
			layer.close(layer.index);
		}
		/**ztree*/
		var ztree = "";
		self.depclick = function(){
			$("#txt_dep").focus();
			$("#treeContent").show();
			self.ztreeshow();
		}
		self.ztreeshow = function(){
			var tree ={
					pFunction: {  
						zTree: '',  
						setting:{ 
							view: {
								selectedMulti: false,
								showIcon: true
							},
							check:{  
								enable:false  
							},    
							data:    {  
							
								simpleData: {
									enable: true,
									idKey: "id",
									pIdKey: "pid",
									rootPId: null
								}  
							},   
							callback: {
								onClick: self.zTreeOnClick
							}
						}
					}
			};
			console.log( 1111);
			$.ajax({
				url:"/sysmenu/doFindZtreeObject",
				type:"post",
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				success:function(resourceInfo){
					var zNodes =  resourceInfo.data;
					ztree = $.fn.zTree.init($("#treeDemo"), tree.pFunction.setting,zNodes);
					ztree.expandAll(true);
					console.log( ztree);
				}
			})
			
		}
		
		/**ztree并获取名字*/
		var node;
		self.zTreeOnClick  = function(event,treeId,treeNode){
			 node = ztree.getSelectedNodes();
			 $("#txt_dep").val(node[0].name);
			self.doHideZtree();
		};
		/**隐藏树结构*/
		self.doHideZtree= function(){
			$("#treeContent").css("display","none");
		}
		/**显示图片*/
		self.showMenu = function(){
			$("#iconUrl").focus();
			$("#iconKunhide").show();
		}
		/**ztree 失去焦点隐藏树结构*/
		self.ztreeblur = function(){ 
			$("#txt_dep").blur(function(){
				setTimeout(function(){
						self.doHideZtree();
				},200)
			});
		}
		
		/**图片选择失去焦点隐藏div*/
		self.iconblur = function(){
			$("#iconUrl").blur(function(){
				 setTimeout(function(){
					 self.imgHide();
			        }, 200);
			});
		}
		/**图片点击事件*/
		self.dd = function(data,obj){
			var path = $(obj.target).attr("src");
			console.log(path)
			$("#iconUrl").val(path);
			self.imgHide();
		}
		/**隐藏图片div*/
		self.imgHide = function(){
			$("#iconKunhide").css("display","none");
		}
	}
	var model = new ViewModel();
	ko.applyBindings(model);
});

